require 'quote_unquote'

class BaseModel < ActiveRecord::Base
  self.abstract_class = true

  class << self
    attr_accessor :export_table_name
    attr_accessor :export_mappings
  end

  def to_sql
    table = self.class.export_table_name
    "insert into #{table}(#{export_fields}) values(#{export_values});\n"
  end

  private

  def export_fields
    self.class.export_mappings.keys().join(", ")
  end

  def export_values
    fields = []

    self.class.export_mappings.each do |key, field|
      value = self.send(field)

      if value.nil?
        fields << 'NULL'
      else
        fields << ActiveRecord::Base.connection.quote(value.to_s)
      end
    end

    fields.join(', ')
  end
end